<?php

require_once("alipayApi/alipay.config.php");
require_once("alipayApi/lib/alipay_submit.class.php");


ini_set('date.timezone','Asia/Shanghai');
//error_reporting(E_ERROR);

require_once "WxpayAPI_php_v3/lib/WxPay.Api.php";
require_once "WxpayAPI_php_v3/example/WxPay.NativePay.php";
require_once 'WxpayAPI_php_v3/example/log.php';

//请求生成订单
session_start();
$info = $_SESSION['submit-info'] ;

$res = file_get_contents("http://120.25.126.95:8033/cms/shop/shopMifiCardOrder/create.do?shopMifiCardId={$info['card-type']}&count={$info['card-count']}");
//保存地址
if (empty($res)) {
    echo json_encode(array('success' => false, 'message' => '网络错误'));
    exit;
}
$orderInfo = json_decode($res, true);
if (empty($orderInfo['result'])) {
    echo json_encode(array('success' => false, 'message' => 'MIFI卡不存在'));
    exit;
}

$url  =  "http://120.25.126.95:8033/cms/shop/shopAddress/create.do?"
        . "provinceId={$info['area-province-id']}"
        . "&provinceName={$info['area-province']}"
        . "&cityId={$info['area-city-id']}&"
        . "cityName={$info['area-city']}&"
        . "townId={$info['area-district-id']}&townName={$info['area-district']}&address={$info['card-type']}"
        . "&consignee={$info['area-consignee']}&phone={$info['area-mobile']}&orderNo={$orderInfo['result']['orderNo']}";

$res = file_get_contents($url);
$res = json_decode($res, true);
if (empty($orderInfo['result'])) {
    echo json_encode(array('success' => false, 'message' => '网络错误'));
    exit;
}


/* * ************************请求参数************************* */
//商户订单号，商户网站订单系统中唯一订单号，必填
//$out_trade_no = $_POST['WIDout_trade_no'];
$out_trade_no = $orderInfo['result']['orderNo'];

//订单名称，必填
//$subject = $_POST['WIDsubject'];
$subject =$info['card-title']  ;
//付款金额，必填0.01;//$info['card-count']*$info['card-price']+9;
$total_fee = $orderInfo['result']['total'];

//商品描述，可空
$body =$orderInfo['result']['tip'];




if($info['pay-way'] == 1){

  /**
   * 流程：
   * 1、调用统一下单，取得code_url，生成二维码
   * 2、用户扫描二维码，进行支付
   * 3、支付完成之后，微信服务器会通知支付成功
   * 4、在支付成功通知中需要查单确认是否真正支付成功（见：notify.php）
   */
//  $attach = $_POST['phone'].';'.$_POST['rechargeItemId'];//卡号和要充值的套餐
  $notify = new NativePay();
  $input = new WxPayUnifiedOrder();
  $input->SetBody($subject);
//  $input->SetAttach($attach);//out_trade_no
  //WxPayConfig::MCHID.date("YmdHis")
  $input->SetOut_trade_no($out_trade_no);
  $input->SetTotal_fee($total_fee*100);
  $input->SetTime_start(date("YmdHis"));
  $input->SetTime_expire(date("YmdHis", time() + 600));
  $input->SetGoods_tag($orderInfo['result']['typeName'].$orderInfo['result']['itemName']);
  $input->SetNotify_url("http://kitty.zicp.net/chongzhi/WxpayAPI_php_v3/example/notify.php");
  $input->SetTrade_type("NATIVE");
  $input->SetProduct_id("123456789");
//  $input->SetAttach($attach);
  $result = $notify->GetPayUrl($input);
  $result['out_trade_no'] = $input->GetOut_trade_no();
  echo json_encode($result);

}else{
  /* * ********************************************************* */

  //构造要请求的参数数组，无需改动
  $parameter = array(
      "service" => $alipay_config['service'],
      "partner" => $alipay_config['partner'],
      "seller_id" => $alipay_config['seller_id'],
      "payment_type" => $alipay_config['payment_type'],
      "notify_url" => $alipay_config['notify_url'],
      "return_url" => $alipay_config['return_url'],
      "anti_phishing_key" => $alipay_config['anti_phishing_key'],
      "exter_invoke_ip" => $alipay_config['exter_invoke_ip'],
      "out_trade_no" => $out_trade_no,
      "subject" => $subject,
      "total_fee" => $total_fee,
      "body" => $body,
      "_input_charset" => trim(strtolower($alipay_config['input_charset']))
          //其他业务参数根据在线开发文档，添加参数.文档地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.kiX33I&treeId=62&articleId=103740&docType=1
          //如"参数名"=>"参数值"
  );

  //建立请求
  $alipaySubmit = new AlipaySubmit($alipay_config);
  $paramString = $alipaySubmit->alipay_gateway_new.$alipaySubmit->buildRequestParaToString($parameter);
  header("Location: $paramString");


}
